#include <stdio.h>
#include <stdlib.h>

#include "securec.h"
#include "treeUtils.h"

struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize) {
    int *stack = calloc(numsSize, sizeof (int));
    struct TreeNode **tree = calloc(numsSize, sizeof (struct TreeNode *));
    int top = 0;
    for (int i = 0; i < numsSize; ++i) {
        tree[i] = calloc(1, sizeof (struct TreeNode));
        tree[i]->val = nums[i];
        while (top && nums[i] > nums[stack[top - 1]]) {
            tree[i]->left = tree[stack[--top]];
        }

        if (top > 0) {
            tree[stack[top - 1]]->right = tree[i];
        }
        stack[top++] = i;
    }
    int root = stack[0];
    free(stack);
    return tree[root];
}

int main() {
    char str[] = "[1,3,null,null,2]";
    printf("%s\n", "start strtok string.");
    int count = 0;
    char retStrArr[100][10] = {0};
    getNum(str, retStrArr, &count);
    int numArr[9] = {0};
    for (int i = 0; i < count; ++i) {
        printf("%s\n", retStrArr[i]);
        numArr[i] = atoi(retStrArr[i]);
    }
    printf("%s\n", "end strtok string.");

    struct TreeNode *root = malloc(sizeof(struct TreeNode));
    root->val = numArr[0];
    root->left = NULL;
    root->right = NULL;
    bfsBuildTree(root, numArr, count);

    int test[6] = {3,2,1,6,0,5};
    struct TreeNode* ret =  constructMaximumBinaryTree(test, 6);
}